<template>
  <a-modal
    title="新增进场分包商"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item
              label="分公司"
              :labelCol="{lg: {span: 7}, sm: {span:7}}"
              :wrapperCol="{lg: {span: 17}, sm: {span: 17} }">
              <a-select @change="handleChangeCompany"  v-decorator="['branchId',{rules: [{   message: ' ' }]}]"  placeholder="请选择"
                        default-value="0">
                <a-select-option v-for="item in companyNames" :key="item.code" :value="item.code">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-item
              label="项目"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <a-select  @change="handleChangeProject"  v-decorator="['projectId',{rules: [{   message: ' ' }]}]" placeholder="请选择"
                        default-value="0">
                <a-select-option v-for="item in projectNames" :key="item.code" :value="item.code">{{item.name}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item
              label="分包商名称"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <BaseInput
                      v-decorator="['subcontractorId',{rules: [{ required: true, message: ' ' }]}]"
                      placeholder=" " />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-item
                    label="合作事项"
                    :labelCol="{lg: {span: 7}, sm: {span: 7}}"
                    :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <BaseInput
                      v-decorator="['matters',{rules: [{ required: true, message: ' ' }]}]"
                      placeholder=" " />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item
                    label="进场时间"
                    :labelCol="{lg: {span: 7}, sm: {span: 7}}"
                    :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <a-date-picker @change="inTime" v-decorator="['entryTime',{rules: [{ required: true, message: ' ' }]}]" style="width: 100%;" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-item
                    label="现场审批表"
                    :labelCol="{lg: {span: 7}, sm: {span: 7}}"
                    :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <!--<a-upload-->
                      <!--name="file"-->
                      <!--:multiple="true"-->
                      <!--action="https://www.mocky.io/v2/5cc8019d300000980a055e76"-->
                      <!--:headers="headers"-->
                      <!--@change="handleChange">-->
                <!--<a-button> <a-icon type="upload" /> 上传附件 </a-button>-->
              <!--</a-upload>-->
              <l-table @fileArrysData="fileArrysData1"   :businessCode="businessCode1"></l-table>
              <!--<a-date-picker @change="outTime" v-decorator="['outTime',{rules: [{ required: true, message: ' ' }]}]" style="width: 100%;" />-->
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item
                    label="定标文件"
                    :labelCol="{lg: {span: 7}, sm: {span: 7}}"
                    :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <!--<a-upload-->
                      <!--name="file"-->
                      <!--:multiple="true"-->
                      <!--action="https://www.mocky.io/v2/5cc8019d300000980a055e76">-->
                <!--<a-button> <a-icon type="upload" /> 上传附件 </a-button>-->
              <!--</a-upload>-->
              <l-table @fileArrysData="fileArrysData2"   :businessCode="businessCode2"></l-table>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-item
                    label="定标时间"
                    :labelCol="{lg: {span: 7}, sm: {span: 7}}"
                    :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <a-date-picker @change="calibrationTime"  style="width: 100%;" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getUserBranch, getUserProject   } from '@/api/project/projects'
// import { themeChoose } from '@/mixin/mixin'

// 表单字段
const fields = ['branchId','projectId','subcontractorId','matters','entryTime', 'outTime']

export default {
  name:'a'+Date.now(),
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      businessCode1:'PM05',
      businessCode2:'PM06',
      companyNames: [], // 分公司名称
      projectNames: [], // 项目名称
      form: this.$form.createForm(this)
    }
  },
  created () {

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
      console.log("返回劳务分包商数据123：",this.model)
    })
    this.getUserBranch();
  },
  methods:{
    handleChangeCompany(val){
      console.log("返回具体公司数据",val)
    },
    fileArrysData1(val){
      this.model.fileArrays1 = val
    },
    fileArrysData2(val){
      this.model.fileArrays2 = val
    },
    getUserBranch() {    // 根据用户查询分公司
      getUserBranch().then(res => {
        this.companyNames = res.data
      })
    },
    getProjectList:function(val){
      this.model.projectList = val;
    },
    getProjectById(value) { // 根据分公司查询项目
      getUserProject(value).then(res => {
        this.projectNames = res.data
      })
    },
    inTime(val){
      this.$emit('inTime',val.format('YYYY-MM-DD'))
    },
    outTime(val){
      this.$emit('outTime',val.format('YYYY-MM-DD'))
    },
    calibrationTime(val){
      this.$emit('calibrationTimes',val.format('YYYY-MM-DD'))
    }

  }
}
</script>
